package com.bipa.controller;

import com.bipa.bean.User;
import com.bipa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/userList")
    public ModelAndView toUserList(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("users",userService.getAllManageUser());
        mv.addObject("highLight", "userty");
        mv.setViewName("back/user_list");
        return mv;
    }

    @RequestMapping("showself")
    public ModelAndView showself(@SessionAttribute(value = "session_user",required = false) User user){
        ModelAndView mv = new ModelAndView();
        //判断用户是否为空，为空说明用户暂未登录
        if(user == null) {
            //跳转至登录页面
            mv.addObject("msg", "您尚未登录，请登录后在进行该操作！");
            mv.setViewName("front/login");
        }else{
            mv.addObject("user",userService.findById(user.getId()));
            mv.addObject("highLight","selfby");
            mv.setViewName("front/self");
        }
        return mv;
    }

    @RequestMapping("updateUser")
    public ModelAndView updateUser(@ModelAttribute User user, SessionStatus sessionStatus){
        ModelAndView mv = new ModelAndView();
        sessionStatus.setComplete();
        userService.update(user);
        mv.addObject("session_user",user);
        mv.setViewName("redirect:./showself");
        return mv;
    }
}
